package chatroom;

import java.util.List;
import java.util.Vector;

import chatroom.domain.User;

public class UserDatabase {

	private static UserDatabase userDatabase = new UserDatabase();
	
	private List<User> loggedInUsers = new Vector<User>(); //TODO tal vez sea mejor utilizar un map

	/**
	 * This class is a singleton
	 */
	private UserDatabase() {
	}

	public static UserDatabase getInstance() {
		return userDatabase;
	}

	public List<User> getLoggedInUsers(String myName) {
		List<User> result = new Vector<User>(); 
		for(User user : loggedInUsers){
			if(!user.getName().equals(myName)){
				result.add(user);
			}
		}
		return result;
	}

	public boolean isUserLogged(String userName) {
		for(User user : loggedInUsers){
			if(user.getName().equals(userName)){
				return true; 
			}
		}		
		return false; 
	}
	
	public void login(User user) {
		loggedInUsers.add(user);
	}

	public void logout(String userName) {
		for(User user : loggedInUsers){
			if(user.getName().equals(userName)){
				loggedInUsers.remove(user);
				break; 
			}
		}
	}
	
	public User getUserByName(String userName){
		for(User user : loggedInUsers){
			if(user.getName().equals(userName)){
				return user;
			}
		}
		return null; 
	}
}
